package com.vipsoft.base.handler;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.lang.reflect.Field;

@MappedTypes({Object.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MySqlJosnTypeHandler extends AbstractJsonTypeHandler<Object> {

    public MySqlJosnTypeHandler(Class<?> type) {
        super(type);
    }

    public MySqlJosnTypeHandler(Class<?> type, Field field) {
        super(type, field);
    }

    @Override
    public Object parse(String json) {
        try {
            return JSON.parseObject(json, this.getFieldType());
        } catch (Exception ex) {
            return json;
        }
    }

    @Override
    public String toJson(Object obj) {
        return JSON.toJSONString(obj);
    }

}
